<?php
/**
 * @Author:      zk
 * @DateTime:    18-7-24 下午2:35
 * @Description: 审批模型审批人配置表相关
 */

class OAApproverConfig extends Eloquent
{
    protected $table = 'oa_approver_config';

    /**
     * @Author:      zk
     * @DateTime:    18-7-27 上午11:31
     * @Description: 根据审批模板id获取该审批模板的抄送人员 [用户信息]
     * @param int $approval_model_id 审批模板id
     * @return array
     */
    public static function getCopyPeopleByApprovalModelId($approval_model_id){
        $select_arr = [
            'oa_approver_config.user_id', 'oa_user.hrm_user_id', 
        ];
        $info = OAApproverConfig::leftJoin('oa_user', 'oa_approver_config.user_id', '=', 'oa_user.id')
                ->where('oa_approver_config.approval_model_id', $approval_model_id)
                ->where('oa_approver_config.user_type', 2)
                ->where('oa_approver_config.status', 1)
                ->select($select_arr)
                ->get()
                ->toArray();
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-7-27 下午2:45
     * @Description: 根据审批条件组id获取相应的人员及角色
     * @param int $rule_group_id 审批条件组id
     * @return array        
     */
    public static function getApproverByRuleGroupId($rule_group_id){
        $result = [];
        $user_ids = [];
        $role_ids = [];
        $select_arr = [
            'approval_model_id', 'type', 'user_type', 'user_id', 'role_id', 'rule_group_id', 
        ];
        $list = OAApproverConfig::where('status', 1)
                ->where('user_type', 1)
                ->where('rule_group_id', $rule_group_id)
                ->select($select_arr)
                ->get()
                ->toArray();
        if(empty($list)){
            return $result;
        }
        
        foreach ($list as $k => $v) {
            if($v['type'] == 1){
                //用户
                $ext = OAUser::getUserInfo($v['user_id']);
                if(isset($ext['role'])){
                    unset($ext['role']);    //去除该用户角色信息
                }
                $user_ids[] = $ext['hrm_user_id'];    //hrm平台用户id
            } elseif ($v['type'] == 2){
                //角色
                $ext = OARole::getRoleInfoByRoleid($v['role_id']);
                $role_ids[] = $v['role_id'];
            }
            $temp = $v;
            $temp['ext'] = $ext;
            
            $result[] = $temp;
        }
        $info = [
            'list' => array_values($result),
            'role_ids' => $role_ids,
            'user_ids' => $user_ids,
        ];
        return $info;
    }
    
    /**
     * @Author:      zk
     * @DateTime:    18-8-2 上午10:15
     * @Description: 根据审批条件组id获取所有的审批人员的用户id数组【如果用重复的用户则去重】 [ 如果存在角色，将角色用户转换成用户信息 ]
     * @param int $rule_group_id 审批条件组id
     * @param int $user_id OA申请人用户ID
     * @return array        
     */
    public static function getApproverUserInfoByRuleGroupId($rule_group_id, $user_id){
        $result = [];
        //获取用户信息
        $user_info = OAUser::getUserInfo($user_id);
        if(empty($user_info)){
            return $result;
        }
        $hr_department_id = $user_info['hr_department_id']; //申请人的hrm平台部门ID
        
        $select_arr = [
            'approval_model_id', 'type', 'user_type', 'user_id', 'role_id', 'rule_group_id', 
        ];
        $list = OAApproverConfig::where('status', 1)
                ->where('user_type', 1)
                ->where('rule_group_id', $rule_group_id)
                ->select($select_arr)
                ->get()
                ->toArray();
        if(empty($list)){
            return $result;
        }
        foreach ($list as $k => $v) {
            if ($v['type'] == 2){
                //根据角色id获取该角色下的所有有效用户
                $user_info = OAUserRole::getUserByRoleid($v['role_id'], [1]);
                if(!empty($user_info)){
                    if(count($user_info) == 1){
                        //如果该角色里只有一个用户
                        foreach ($user_info as $k_2 => $v_2) {
                            $result[] = $v_2['id'];
                        }
                    } else {
                        //如果该角色里用户人数大于1 需要判断审批人的部门ID是否和申请人的部门ID一致
                        $hrm_user_id_arr = [];  //hrm用户ID数组
                        foreach ($user_info as $k_2 => $v_2) {
                            $hrm_user_id_arr[] = $v_2['hrm_user_id'];
                        }
                        $hrm_user_id_info = HrmUser::getHrmUserInfoByHrmUserid($hrm_user_id_arr);
                        if(empty($hrm_user_id_info)){
                            continue;
                        }
                        foreach ($user_info as $k_2 => $v_2) {
                            //审批人 hrm平台部门ID
                            $approver_hr_department_id = isset($hrm_user_id_info[$v_2['hrm_user_id']]['department_id']) ? $hrm_user_id_info[$v_2['hrm_user_id']]['department_id'] : 0;
                            if($hr_department_id == $approver_hr_department_id){
                                $result[] = $v_2['id'];
                            }
                        }
                    }
                }
            } else {
                $result[] = $v['user_id'];
            }
        }
        $result = array_unique($result);
        
        return $result;
    }
    
    
}
